package com.zhl.hive.custom;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

/**
 * @program: demos
add jar /home/zhl/hadoop/modules/hive-3.1.2/lib/hive-1.0-SNAPSHOT.jar;

create temporary function my_len as "com.zhl.hive.custom.LenUdf";




 * @description:
 * @author: 刘振华
 * @create: 2020-11-20 18:20
 **/
public class LenUdf extends GenericUDF {
	/**
	* @Description: 对输入的参数做一个初始化以及检校的工作
	* @Param: 参数
	* @return: 约定返回值
	* @Author: 刘振华
	* @Date: 2020/11/25 13:09
	*/
	@Override
	public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
		if(objectInspectors == null || objectInspectors.length<1){
			throw new UDFArgumentException("参数长度小于1!");
		}
		if(objectInspectors[0].getCategory().equals(ObjectInspector.Category.PRIMITIVE)){
			throw new UDFArgumentException("参数不符合要求!");
		}
		//约定返回值类型
		return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
	}

	/**
	* @Description: 主要逻辑处理
	* @Param: 参数
	* @return: 返回值
	* @Author: 刘振华
	* @Date: 2020/11/25 13:13
	*/
	@Override
	public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
		Object obj = deferredObjects[0].get();
		if(null == obj){
			return 0;
		}
		return obj.toString().length();
	}


	/**
	* @Description: 展示信息
	* @Param: 参数
	* @return:
	* @Author: 刘振华
	* @Date: 2020/11/25 13:14
	*/
	@Override
	public String getDisplayString(String[] strings) {
		return "";
	}
}
